using KrasDD.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace KrasDD.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BaseController<T> : ControllerBase where T : class
    {
        protected readonly IBaseService<T> _service;
        
        public BaseController(IBaseService<T> service)
        {
            _service = service;
        }
        
        [HttpGet("{id}")]
        public virtual async Task<T> Get(string id)
        {
            return await _service.GetByIdAsync(id);
        }
        
        [HttpGet]
        public virtual async Task<List<T>> GetAll()
        {
            return await _service.GetAllAsync();
        }
        
        [HttpPost]
        public virtual async Task<T> Create([FromBody] T entity)
        {
            return await _service.CreateAsync(entity);
        }
        
        [HttpPut]
        public virtual async Task<T> Update([FromBody] T entity)
        {
            return await _service.UpdateAsync(entity);
        }
        
        [HttpDelete("{id}")]
        public virtual async Task<bool> Delete(string id)
        {
            return await _service.DeleteAsync(id);
        }
    }
}
